local skel = fk.CreateSkill {
  name = "lb__shijian",
  tags = { Skill.Limited },
}

Fk:loadTranslationTable {
  ["lb__shijian"] = "誓剑",
  --[":lb__shijian"] = "<b>限定技</b>，结束阶段，你可以将至多存活角色数张牌置于武器牌上，称为「<b>光辉</b>」。你的攻击范围与【杀】的伤害增加武器上的「<b>光辉</b>」数，你使用【杀】后移除所有「<b>光辉</b>」。",
  --10.16
  [":lb__shijian"] = "<b>限定技</b>，结束阶段，你可以摸本回合<a href=':lb__fengjie'><font color='red'>〖风界〗</font></a>的发动次数张牌，然后将至多等量牌置于武器牌上，称为「<b>光辉</b>」。你的攻击范围与【杀】的伤害增加武器上的「<b>光辉</b>」数，你使用【杀】后移除所有「<b>光辉</b>」。",

  ["#lb__shijian_draw"] = "誓剑：你可以摸%arg张牌，然后将至多 %arg 张牌置于武器牌上，强化攻击范围与【杀】",
  ["#lb__shijian"] = "誓剑：你可以将至多 %arg 张牌置于 %arg2 上，强化攻击范围与【杀】",
  ["#lb__shijian_weapon"] = "誓剑：你可以将至多 %arg 张牌置于武器牌上，强化攻击范围与【杀】，请选择一张武器牌",

  ["lb__shijian_guang"] = "光辉",

  ["$lb__shijian1"] = "以星辰之光，点亮大地！",
  ["$lb__shijian2"] = "Ex—calibur！！",
}

skel:addEffect(fk.EventPhaseEnd, {
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(skel.name) and player:usedSkillTimes(skel.name, Player.HistoryGame) == 0
        and target == player and data.phase == Player.Finish --and #player:getCardIds("he") > 1
        and player:usedSkillTimes("lb__fengjie", Player.HistoryTurn) > 0
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askToSkillInvoke(player, {skill_name = skel.name, prompt = "#lb__shijian_draw:::" .. player:usedSkillTimes("lb__fengjie", Player.HistoryTurn)})
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:notifySkillInvoked(player, skel.name)
    player:broadcastSkillInvoke(skel.name, 1)
    local num = player:usedSkillTimes("lb__fengjie", Player.HistoryTurn)
    player:drawCards(num, skel.name)
    local weapons = table.filter(player:getCardIds("e"), function(id)
      local card = Fk:getCardById(id)
      return card.sub_type == Card.SubtypeWeapon and #card:getTableMark(skel.name) == 0
    end)
    if #weapons == 0 then return end
    local weapon = weapons[1]
    if #weapons > 1 then
      local cards = room:askToCards(player, {
        min_num = 1,
        max_num = 1,
        skill_name = skel.name,
        pattern = tostring(Exppattern { id = weapons }),
        cancelable = true,
        include_equip = true,
        prompt = "#lb__shijian_weapon:::" .. num,
      })
      if #cards > 0 then
        weapon = cards[1]
      else
        return
      end
    end
    weapon = Fk:getCardById(weapon)
    local cards = room:askToCards(player, {
      min_num = 1,
      max_num = num,
      skill_name = skel.name,
      pattern = ".|.|.|.|.|.|^(" .. weapon.id .. ")",
      cancelable = true,
      include_equip = true,
      prompt = "#lb__shijian:::" .. num .. ":" .. weapon:toLogString(),
    })
    if #cards > 0 then
      room:setCardMark(weapon, skel.name, cards)
      room:setPlayerMark(player, skel.name, weapon.id)
      player:addToPile("lb__shijian_guang", cards, false, skel.name, player)
    end
  end,
})


skel:addEffect(fk.AfterCardsMove, {
  global = true,
  can_refresh = function(self, event, target, player, data)
    if #player:getPile("lb__shijian_guang") == 0 then return false end
    for _, move in ipairs(data) do
      for _, info in ipairs(move.moveInfo) do
        local card = Fk:getCardById(info.cardId)
        if #card:getTableMark(skel.name) > 0 then
          if info.fromArea == Card.Processing then
            local room = player.room
            --注意到一次交换事件的过程中的两次移动事件都是在一个parent事件里进行的，因此查询到parent事件为止即可
            local move_event = room.logic:getCurrentEvent():findParent(GameEvent.MoveCards, true)
            if not move_event then return end
            local parent_event = move_event.parent
            local move_events = room.logic:getEventsByRule(GameEvent.MoveCards, 1, function(e)
              if e.id >= move_event.id or e.parent ~= parent_event then return false end
              for _, last_move in ipairs(e.data) do
                if last_move.moveReason == fk.ReasonExchange and last_move.toArea == Card.Processing then
                  return true
                end
              end
            end, parent_event.id)
            if #move_events > 0 then
              for _, last_move in ipairs(move_events[1].data) do
                if last_move.moveReason == fk.ReasonExchange then
                  for _, last_info in ipairs(last_move.moveInfo) do
                    local card_ = Fk:getCardById(info.cardId)
                    if #card_:getTableMark(skel.name) > 0 then
                      if last_move.from == player and last_info.fromArea == Card.PlayerEquip then
                        if move.toArea == Card.PlayerEquip then
                          if move.to ~= player then
                            event:setCostData(self, { extra_data = move.to })
                            return true
                          end
                        else
                          event:setCostData(self, nil)
                          return true
                        end
                      end
                    end
                  end
                end
              end
            end
          elseif move.moveReason == fk.ReasonExchange then
            if move.from == player and info.fromArea == Card.PlayerEquip and move.toArea ~= Card.Processing then
              --适用于被修改了移动区域的情况，如销毁，虽然说原则上移至处理区是不应销毁的
              event:setCostData(self, nil)
              return true
            end
          elseif move.from == player and info.fromArea == Card.PlayerEquip then
            if move.toArea == Card.PlayerEquip then
              if move.to ~= player then
                event:setCostData(self, { extra_data = move.to })
                return true
              end
            else
              event:setCostData(self, nil)
              return true
            end
          end
        end
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local cards = player:getPile("lb__shijian_guang")
    if event:getCostData(self) ~= nil then
      event:getCostData(self).extra_data:addToPile("lb__shijian_guang", cards, false, skel.name)
    else
      room:moveCardTo(cards, Card.DiscardPile, nil, fk.ReasonPutIntoDiscardPile, skel.name, nil, true)
    end
  end
})

skel:addEffect(fk.CardUsing, {
  global = true,
  is_delay_effect = true,
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and data.card.trueName == "slash"
        and #player:getPile("lb__shijian_guang") > 0 and player:getMark(skel.name) ~= 0
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:notifySkillInvoked(player, skel.name, "big")
    player:broadcastSkillInvoke(skel.name, 2)
    local cards = player:getPile("lb__shijian_guang")
    data.additionalDamage = (data.additionalDamage or 0) + #cards
    local card = Fk:getCardById(player:getMark(skel.name))
    if card then
      room:setCardMark(card, skel.name, 0)
      room:setPlayerMark(player, skel.name, 0)
    end
    room:moveCardTo(cards, Card.DiscardPile, nil, fk.ReasonPutIntoDiscardPile, skel.name, nil, true)
  end,
})

skel:addEffect("atkrange", {
  global = true,
  correct_func = function(self, from, to)
    if from:getMark("lb__shijian") ~= 0 then
      return #from:getPile("lb__shijian_guang")
    end
  end,
})


return skel